package com.alibaba.system.controller;


import com.alibaba.basic.uitl.AjaxResult;
import com.alibaba.basic.uitl.PageList;
import com.alibaba.system.domain.Detail;
import com.alibaba.system.domain.Type;
import com.alibaba.system.query.DetailQuery;
import com.alibaba.system.query.TypeQuery;
import com.alibaba.system.service.DetailService;
import com.alibaba.system.service.TypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/type")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class TypeController {

    @Autowired
    private TypeService typeService;

    @Autowired
    private DetailService detailService;

    @GetMapping
    public List<Type> findAll(){
       return typeService.findAll();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Type findById(@ApiParam(value="类型ID",required = true)  @PathVariable("id") Long id) {
        return typeService.finById(id);
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Type type){
        if (null == type.getId()){
            typeService.add(type);
        } else {
            typeService.update(type);
        }
        return AjaxResult.me().success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        typeService.delete(id);
        detailService.deleteByTypesId(id);
        return AjaxResult.me().success();
    }
    /**
     * 分页查询
     */
    @PostMapping
    public PageList<Type> queryByPage(@RequestBody TypeQuery query) {
        return typeService.queryByPage(query);
    }

    /**
     *  批量删除
     */
    @PatchMapping
    public AjaxResult deleteId(@RequestBody List<Long> ids) {
        typeService.deleteId(ids);
        return AjaxResult.me().success();
    }



}
